/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import {Mongo} from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
import {Meteor} from 'meteor/meteor';

//define RegisterUser fields
class RegisterUserCollection extends Mongo.Collection {
    insert(doc, callback) {
        const ourDoc = doc;
        ourDoc.createdAt = ourDoc.createdAt || new Date();
        let result = undefined;
        if (super.find({account_address: ourDoc.account_address}).count() > 0){
            result = super.update({account_address:ourDoc.account_address},{$set:ourDoc})
        }else{
            result = super.insert(ourDoc, callback);
        }
        return result;
    }

    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }

    remove(selector) {
        const result = super.remove(selector);
        return result;
    }
}

export const RegisterUser = new RegisterUserCollection('register_users');

// Deny all client-side updates since we will be using methods to manage this collection
RegisterUser.deny({
    insert() {
        return true;
    },
    update() {
        return true;
    },
    remove() {
        return true;
    },
});

RegisterUser.schema = new SimpleSchema({
    phone: {// As user account id
        type: String,
    },

    user_certificate_pem: { // Users' pem format certificate which needed to be sent to a repchain node
        // Which can register the user into repchain
        type: String,

    },

    address: {
        type: String,
    },

    unit: {
        type: String,
        optional: true,
    },

    account_address: {
        type: String,
        defaultValue: 'none',
    },

    credential_type: {
        type: Number,
        optional: true,
    },

    credential_number: {
        type: String,
        max: 100,
        optional: true,
    },

    name: {
        type: String,
        max: 100,
    },

    email: {
        type: String,
        max: 100,
    },

    createdAt: {
        type: String,
    },

    status: {
        type: Boolean,
        defaultValue: false,
    },

    avatar: {
        type: String,
        optional: true,
    },

    isAdmin: {
        type: Boolean,
        defaultValue: false,
    },
});

RegisterUser.attachSchema(RegisterUser.schema);

RegisterUser.publicFields = {
    account_address: 1,
    name: 1,
};


if (Meteor.isServer) {
    // This code only runs on the server

    Meteor.publish('registerUsers', (query, limit, skip)=>{
        let users = RegisterUser.find(query,{sort: { createdAt: -1 }, limit:limit, skip:skip });
        return users;
    });

    Meteor.publish('registerUser',(query)=>{
        let res = RegisterUser.find(query);
        return res;
    });
}